/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.castannon.axis2.guice;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.OperationContext;
import org.apache.axis2.context.ServiceContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.engine.ServiceLifeCycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Injector;

/**
 * 
 * @author David Castañón (dcastannon)
 *
 */
public abstract class AbstractGuiceServiceInit implements ServiceLifeCycle {
    private final static Logger log = LoggerFactory
	    .getLogger(AbstractGuiceServiceInit.class);

    public OMElement guiceInit(OMElement ignore) {
	return null;
    }

    public void init(ServiceContext serviceContext) {

    }

    public void setOperationContext(OperationContext arg0) {

    }

    public void destroy(ServiceContext arg0) {

    }

    public void startUp(ConfigurationContext ignore, AxisService service) {
	beforeStartUp();
	log.debug("startUp");
	String injectorKey = "defaultInjector";
	{
	    Parameter parameter = service.getParameter("InjectorKey");
	    if (parameter != null)
		injectorKey = ((String) parameter.getValue()).trim();
	}
	log.debug("The injector key is {}", injectorKey);

	GuiceInjectorHolder.setInjector(injectorKey, createInjector(service));
	
	afterStartUp();
    }

    public void afterStartUp() {
    }

    public void beforeStartUp() {
    }

    public abstract Injector createInjector(AxisService service);

    public void shutDown(ConfigurationContext ignore, AxisService service) {
	log.debug("shutDown");

    }

}
